//
//  BookJourneyDetailController.m
//  FreeTrip
//
//  Created by 352324497@qq.com on 15/11/27.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "BookJourneyDetailController.h"
#import "BookJournDetailDayModel.h"
#import "BookJournDetailNodesModel.h"
#import "BookJourneyDetailModel.h"
#import "BookJourneySectionCell.h"
#import "BookJourneyDetailSecondCell.h"
#import "BookJourneyDetailFirstCell.h"
#import "BookPlaceDetailController.h"
@interface BookJourneyDetailController ()

@property (nonatomic, strong)BookJourneyDetailModel * detailModel;
@property (nonatomic, strong)NSMutableArray *daysArray;
//@property (nonatomic, strong)NSMutableArray *plansArray;


@end

@implementation BookJourneyDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.model.name;
    self.tableView.separatorStyle = NO;
    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/plans/%ld.json",(long)self.model.Id];
    self.daysArray = [NSMutableArray array];
    
    [[NetworkState shareInstance] reachablityStates:self];

    [[NetworkRequest shareNetworeRequest]netRequestForBookjournDetailWithUrl:url networkRequest:^(id model) {
        self.detailModel = model;
        dispatch_async(dispatch_get_main_queue(), ^{
            self.daysArray = self.detailModel.daysArray;
            [self.tableView reloadData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        });
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.daysArray.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0)
    {
        return 1;
    }
    else
    {
        BookJournDetailDayModel *model = self.daysArray[section - 1];
        NSMutableArray *plansArray = [NSMutableArray array];
        plansArray = model.planArray;
        return plansArray.count + 1;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return [BookJourneyDetailFirstCell heightForCell:self.model];
    }
    else
    {
        if (indexPath.row == 0)
        {
            BookJournDetailDayModel *model = self.daysArray[indexPath.section - 1];
            return [BookJourneyDetailSecondCell heightForCell:model];
        }
        else
        {
            BookJournDetailDayModel *model = self.daysArray[indexPath.section - 1];
            NSMutableArray *plansArray = [NSMutableArray array];
            plansArray = model.planArray;
            BookJournDetailNodesModel *plansModel = plansArray[indexPath.row - 1];
            return [BookJourneySectionCell heightForCell:plansModel];
        }
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return NULL;
    }
    else
    {
        BookJournDetailDayModel *model = self.daysArray[section - 1];
        NSMutableArray *plansArray = [NSMutableArray array];
        plansArray = model.planArray;
        BookJournDetailNodesModel *nodesModel = plansArray[0];
        NSString *string = [NSString stringWithFormat:@"DAY%ld %@", (long)section, nodesModel.name_zh_cn];
        return string;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0)
    {
        BookJourneyDetailFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil)
        {
            cell = [[BookJourneyDetailFirstCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
        }
        cell.model = self.model;
        return cell;
    }
    else
    {
        if (indexPath.row == 0)
        {
            BookJourneyDetailSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondCell"];
            if (cell == nil)
            {
                cell = [[BookJourneyDetailSecondCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"secondCell"];
            }
            cell.layer.borderWidth = 0.4;
            cell.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor grayColor]);
            BookJournDetailDayModel *model = self.daysArray[indexPath.section - 1];
            cell.model = model;
            return cell;
        }
        else
        {
            BookJourneySectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sectionCell"];
            if (cell == nil)
            {
                cell = [[BookJourneySectionCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"sectionCell"];
            }
            cell.layer.borderWidth = 0.4;
            cell.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor grayColor]);
            BookJournDetailDayModel *model = self.daysArray[indexPath.section - 1];
            NSMutableArray *plansArray = [NSMutableArray array];
            plansArray = model.planArray;
            BookJournDetailNodesModel *plansModel = plansArray[indexPath.row - 1];
            cell.model = plansModel;
            cell.titleLabel.text = [NSString stringWithFormat:@"第%ld站: %@", (long)indexPath.row, plansModel.entry_name];
            return cell;
        }
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        
    }
    else
    {
        if (indexPath.row == 0)
        {
           
        }
        else
        {
            BookJournDetailDayModel *model = self.daysArray[indexPath.section - 1];
            NSMutableArray *plansArray = [NSMutableArray array];
            plansArray = model.planArray;
            BookJournDetailNodesModel *plansModel = plansArray[indexPath.row - 1];
            BookPlaceDetailController *bookPlaceDetailVC = [[BookPlaceDetailController alloc]initWithStyle:(UITableViewStylePlain)];
            bookPlaceDetailVC.name = plansModel.entry_name;
            bookPlaceDetailVC.Id = plansModel.entry_id;
            bookPlaceDetailVC.latitude = plansModel.lat;
            bookPlaceDetailVC.longitude = plansModel.lng;
            bookPlaceDetailVC.name_zh_cn = plansModel.name_zh_cn;
            [self.navigationController pushViewController:bookPlaceDetailVC animated:YES];
        }
    }

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
